## This program is free software: you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation, either version 3 of the License, or (at
## your option) any later version.

## This program is distributed in the hope that it will be useful, but
## WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
## General Public License for more details.

## You should have received a copy of the GNU General Public License
## along with this program (in the file LICENCE-GPLv3.txt).  If not, see
## <http://www.gnu.org/licenses/>.


from Map import *
from NameFactory import *

import sys


USAGE = """
Incorrect arguments.

Usage:
\tmain.py LATITUDE_COLUMN_INDEX LONGITUDE_COLUMN_INDEX OPTION FILE...


where OPTION is exactly one of the following

BASIC_DISPLAY \t(just draw paths)
ANIMATED_MARKS \t(each second, represent a person's position via a mark)
ANIMATED_MARKS_WITH_TOOLBAR \t(same as ANIMATED_MARKS but with an animation-control toolbar)
ANIMATED_POLYLINES \t(incrementally draw paths in time)
ENCODED_POLYLINES \t(BASIC_DISPLAY but with encoded polylines -- THIS IS BUGGY NOW)
MODIFIABLE_POLYLINES \t(draw paths that can be modified by the user -- THIS IS INTRACTABLE NOW)

        
Example:
\tpython main.py 4 5 ANIMATED_MARKS_WITH_TOOLBAR csv/*
"""

OPTIONS = ['BASIC_DISPLAY', 'MODIFIABLE_POLYLINES', 'ANIMATED_MARKS', 'ANIMATED_MARKS_WITH_TOOLBAR', 'ANIMATED_POLYLINES', 'ENCODED_POLYLINES']

#OPTIONS = ['ANIMATED_MARKS_WITH_TOOLBAR']
if (len(sys.argv[0]) < 5 or sys.argv[0] != "main.py" or (not sys.argv[1].isdigit()) or (not sys.argv[2].isdigit()) or (not (sys.argv[3] in OPTIONS))):
    print USAGE
    quit(-1)


# TODO [LOW PRIORITY] : assume default values 4 & 5 respectivelly => add an argument ``--columns a b'' that sets nondefaults
latitude_column_index = int(sys.argv[1])
longitude_column_index = int(sys.argv[2])

# TODO: these files will eventually come from the cloud (may be nonlocal)
# consider GDownloadUrl() GXmlHttp
csv_files_dirs = sys.argv[4:]     

names = NameFactory(csv_files_dirs, latitude_column_index, longitude_column_index)


# create a Map object
map = Map(sys.argv[3])       

# parse all GPS traces (paths)
for p in names.extractRawPaths():
    map.addPath(p)
    print 'Path ', p.name, ' added (has ', len(p.points), ' points)'   


#print map.to_html()
  
# generate a HTML file
output = map.to_html()
if (map.ANIMATED_MARKS_WITH_TOOLBAR):
    toValidation = open('map.html', 'r').read()
    map.validateHTML(toValidation)
    open('javascript/actrex.js', 'w').write(output)

else:
    print "Since you used one of the older modes ("+sys.argv[3]+"), the output is in map_legacy.html"
    open('map_legacy.html','w').write(output)
